﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;

namespace AmorCanino.Common.Logger
{
    public class FileLogger
        : ILogger
    {

        public void WriteLog(LogEntry logEntry)
        {
            WriteTheLog(logEntry);
        }

        public void WriteLog(LogEntry logEntry, LogLevels logLevel)
        {
            this.LogLevel = logLevel;
            WriteTheLog(logEntry);
        }

        public void WriteLog(LogEntry logEntry, LogLevels logLevel, LogDetails logDetail)
        {
            this.LogLevel = logLevel;
            this.LogDetail = logDetail;
            WriteTheLog(logEntry);
        }

        private void WriteTheLog(LogEntry logEntry)
        {

            try
            {
                StreamWriter writer = null;
                string filePath = ConfigurationHandler.GetFileLoggerPath;

                if (!File.Exists(filePath))
                    writer = File.CreateText(filePath);
                else
                    writer = File.AppendText(filePath);

                writer.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
                writer.WriteLine(LogDetail + " - " + LogLevel);

                writer.WriteLine(logEntry.Message);
                
                if (logEntry.Source != null)
                    writer.WriteLine(logEntry.Source.ToString());

                writer.WriteLine(logEntry.Application);
                writer.WriteLine();

                writer.Flush();
                writer.Close();
            }
            catch (Exception)
            {
                //Swallow this
            }

        }

        public LogLevels LogLevel
        {
            get;
            set;
        }

        public LogDetails LogDetail
        {
            get;
            set;
        }

    }
}
